package com.example.weather_news.adapter;

/**
 * Created by 逝 on 2017/06/18.
 */


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.weather_news.R;
import com.example.weather_news.activity.WeatherSettingActivity;
import com.example.weather_news.db.SaveCityInfo;

import java.util.ArrayList;
import java.util.List;

public class Adapter_AddCity extends BaseAdapter {

    private List<SaveCityInfo> objects = new ArrayList<SaveCityInfo>();

    private Context context;
    private LayoutInflater layoutInflater;

    public Adapter_AddCity(Context context) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    public void setData(List<SaveCityInfo> objects) {
        this.objects.clear();
        this.objects = objects;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public SaveCityInfo getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.list_set_city, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews(getItem(position).getCityName(), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(final String object, final ViewHolder holder) {
        //TODO implement
        holder.tvSetCityName.setText(object);
        holder.ivDeleteCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((WeatherSettingActivity)context).delateCity(object);
            }
        });
    }

    protected class ViewHolder {
        private TextView tvSetCityName;
        private ImageView ivDeleteCity;

        public ViewHolder(View view) {
            tvSetCityName = (TextView) view.findViewById(R.id.tv_set_cityName);
            ivDeleteCity = (ImageView) view.findViewById(R.id.iv_deleteCity);
        }
    }
}
